package com.mkz.jbpm.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mkz.core.common.util.ResponseResult;
import com.mkz.core.common.util.TimeUtils;
import com.mkz.core.log.annotation.SysLog;
import com.mkz.core.security.util.SecurityUtils;
import com.mkz.jbpm.entity.LeaveInfo;
import com.mkz.jbpm.service.LeaveInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;


/**
 * 请假信息
 *
 * @author KuangJianyu
 * @date 2020-03-31 14:27:15
 */
@RestController
@AllArgsConstructor
@RequestMapping("/leaveinfo" )
@Api(value = "leaveinfo", tags = "请假信息管理")
public class LeaveInfoController {

	private final String PROCESS_DEFINE_KEY = "LeaveProceess";
    private final LeaveInfoService leaveInfoService;

    /**
              * 分页查询
     * @param page 分页对象
     * @param leaveInfo 请假信息
     * @return
     */
	@ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public ResponseResult<Page> getLeaveInfoPage(Page page, LeaveInfo leaveInfo) {
    	if(null==leaveInfo) {
    		leaveInfo = new LeaveInfo();
    	}
    	leaveInfo.setLeaveId(SecurityUtils.getUser().getId().longValue());
        return ResponseResult.ok(leaveInfoService.page(page, Wrappers.query(leaveInfo)));
    }

    /**
              * 通过id查询请假信息
     * @param id id
     * @return ResponseResult
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public ResponseResult<LeaveInfo> getById(@PathVariable("id" ) Long id) {
        return ResponseResult.ok(leaveInfoService.getById(id));
    }

    /**
              * 新增请假信息
     * @param leaveInfo 请假信息
     * @return ResponseResult
     */
    @ApiOperation(value = "新增请假信息", notes = "新增请假信息")
    @SysLog("新增请假信息" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('jbpm_leaveinfo_add')" )
    public ResponseResult<Boolean> save(@RequestBody LeaveInfo leaveInfo) {
    	leaveInfo.setLeaveId(SecurityUtils.getUser().getId().longValue());
    	leaveInfo.setLeaveName(SecurityUtils.getUser().getUsername());
    	leaveInfo.setNowStatus("新增");
    	boolean saveResult = leaveInfoService.save(leaveInfo);
    	if(!saveResult) {
    		return ResponseResult.failed("新增失败！");
    	}
    	return ResponseResult.ok(true);
    }

    /**
              * 修改请假信息
     * @param leaveInfo 请假信息
     * @return ResponseResult
     */
    @ApiOperation(value = "修改请假信息", notes = "修改请假信息")
    @SysLog("修改请假信息" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('jbpm_leaveinfo_edit')" )
    public ResponseResult<Boolean> updateById(@RequestBody LeaveInfo leaveInfo) {
        return ResponseResult.ok(leaveInfoService.updateById(leaveInfo));
    }

    /**
              * 通过id删除请假信息
     * @param id id
     * @return ResponseResult
     */
    @ApiOperation(value = "通过id删除请假信息", notes = "通过id删除请假信息")
    @SysLog("通过id删除请假信息" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('jbpm_leaveinfo_del')" )
    public ResponseResult<Boolean> removeById(@PathVariable Long id) {
    	LeaveInfo leaveInfo = leaveInfoService.getById(id);
    	if(null==leaveInfo) {
    		return ResponseResult.failed("记录已删除");
    	}
    	if(!"新增".equals(leaveInfo.getNowStatus())) {
    		return ResponseResult.failed("不是新增状态，不允许删除");
    	}
        return ResponseResult.ok(leaveInfoService.removeById(id));
    }

    /**
              * 提交请假信息
     * @param leaveInfo 请假信息
     * @return ResponseResult
     * @throws Exception 
     */
    @ApiOperation(value = "提交请假信息", notes = "提交请假信息")
    @SysLog("提交请假信息" )
    @PostMapping("/submit/{id}" )
    @PreAuthorize("@pms.hasPermission('jbpm_leaveinfo_submit')" )
    public ResponseResult<Boolean> submit(@PathVariable Long id) throws Exception {
    	LeaveInfo leaveInfo = leaveInfoService.getById(id);
    	if(null==leaveInfo) {
    		ResponseResult.failed("记录已经删除！");
    	}
    	leaveInfo.setNowStatus("已提交");
    	//流程参数，如有
    	long days = TimeUtils.getDistanse(leaveInfo.getStartDate(), leaveInfo.getEndDate())+1;
    	Map<String, Object> vars = new HashMap<>();
		vars.put("days", days);
		return leaveInfoService.startProcess(PROCESS_DEFINE_KEY, leaveInfo, vars);
    }
}
